package com.wsx.sky.http.book;

import com.google.gson.Gson;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.UUID;

/**
 * @author wangsx
 * @Title: com.turing.sboot.controller.BookController
 * @Description: TODO
 * @date 2019/2/20
 */
@RestController
public class BookController {

    @Autowired
    private JedisPool jedisPool;

    private Gson gson = new Gson();

    @RequestMapping(
            value = "/book/rec",
            method = {RequestMethod.POST}
    )
    public String book(@RequestBody Map<String,String> params, HttpServletResponse response) {
        if (null == params || params.isEmpty()) {
            response.setStatus(400);
            return "error";
        }

        Jedis jedis = null;
        String value = null;
        long start = System.currentTimeMillis();
        try {
            jedis = jedisPool.getResource();
            ///Thread.sleep(80);

            Book book = new Book();
            book.setImgId(UUID.randomUUID().toString().replace("-", ""));
            book.setApiKey("test-01");
            book.setModelName(params.get("model"));
            book.setCurrentTime(System.currentTimeMillis());

            byte[] keys = book.getImgId().getBytes("utf-8");
            byte[] datas = getData();
            jedis.set(keys, datas);
            jedis.expire(keys, 5);

            jedis.rpush("universe:book:queue", gson.toJson(book));

            Thread.sleep(104);

            while (true) {
                if (System.currentTimeMillis() - start >= 1450) {
                    response.setStatus(501);
                    return "error";
                }
                value = jedis.get(book.getImgId() + "-rt");
                if (null == value || value.isEmpty()) {
                    Thread.sleep(2);
                    continue;
                }
                return value;
            }

        } catch (Exception e) {
            response.setStatus(500);
            e.printStackTrace();
        } finally {
            if (null != jedis) {
                jedisPool.returnResourceObject(jedis);
            }
        }
        return "success";
    }

    private byte[] getData() {
        byte[] datas = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File("E:\\book\\2136\\0\\IPC_2018-11-30.13.29.31.0912.jpg"));
            datas = new byte[inputStream.available()];
            inputStream.read(datas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (Exception e) {

                }
            }
        }
        return datas;
    }
}

@NoArgsConstructor
class Book {
    @Setter @Getter private String imgId;
    @Setter @Getter private String apiKey;
    @Setter @Getter private long currentTime;
    @Setter @Getter private String modelName;
}
